﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MySql;
using MySql.Data.MySqlClient;
namespace Server
{
    struct LoginID
    {
        public char[] login;
        public int id;
        public LoginID(string Login,int idLogin)
        {
            login=new char[1];           
            id = idLogin; 
            login = LoginfromString(Login);
        }
        public char[] LoginfromString(string loginString)
        {
            char[] logintmp = new char[16];
            loginString.CopyTo(0, logintmp, 0, loginString.Length);
            return logintmp;
        }
        public string Login
        {
            get
            {
                return new string(login).Replace("\0", "");
            }
        }
    }
    internal class AccauntManager
    {
        Dictionary<string, int> baselogin;
        string fileName="base/logins.zdf";
        nowaEntities DB;
        object locer = new object();
        MySql.Data.MySqlClient.MySqlConnection connect;
        public AccauntManager()
        {
            baselogin = new Dictionary<string, int>(0);
           // string connectionString = "data source=localhost; user id=root; password=963258; database=nowa";
          //  connect = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
            DB = new nowaEntities();
          
          
            loadBase(connect);
        }

        private void checkPath()
        {
            DirectoryInfo dir = new DirectoryInfo("base");
            if (!dir.Exists)
            {
                dir.Create();
            }
            
            FileInfo fi = new FileInfo(fileName);
            if (!fi.Exists)
            {
                fi.Create();
            }
            
        }
        public void loadBase()
        {
            using (FileStream fs = File.OpenRead(fileName))
            {
                int count = (int)fs.Length / (16 * 2 + 20 * 2);
                byte[] bufer = new byte[16 * 2];
                for (int i = 0; i < count; i++)
                {
                    fs.Read(bufer, 0, 16 * 2);
                    fs.Seek(( 20 * 2),SeekOrigin.Current);
                    LoginID str=new LoginID(){login=Encoding.ASCII.GetChars(bufer, 0, 16 * 2),id=i};
                    baselogin.Add(str.Login, str.id);
                }
            }
            

        }
        public void loadBase(MySql.Data.MySqlClient.MySqlConnection con)
        {
         // MySqlDataReader myReader;
         // MySqlCommand myCommand = new MySqlCommand("SELECT * FROM nowa.masteraccaunts", con);
            var query = from accaunts in DB.masteraccaunts
                        select accaunts;


        //  myReader = myCommand.ExecuteReader();
        // while (myReader.Read())
            foreach (var item in query)
            
	{
                
        LoginID str = new LoginID() { login = item.Login.ToCharArray(), id = item.idMasterAccaunts };
              baselogin.Add(str.Login, str.id);
	}

        
               
                   // LoginID str = new LoginID() { login = Encoding.ASCII.GetChars(bufer, 0, 16 * 2), id = i };
                   // baselogin.Add(str.Login, str.id);
               


        }
        public void AddInBase(char[] login, char[] pass)
        {
            if (!baselogin.ContainsKey(new string(login).Replace("\0", "")))
            {
                
            
            //using (FileStream fs = File.OpenWrite(fileName))
            //{

            //    int count = baselogin.Count;
                lock (locer)
                {
                    //byte[] tmp = new byte[16 * 2 + 20 * 2];
                    //Encoding.ASCII.GetBytes(login, 0, login.Length, tmp, 0);
                    //Encoding.ASCII.GetBytes(pass, 0, pass.Length, tmp, 16 * 2);
                    //fs.Seek(count * (tmp.Length), SeekOrigin.Begin);
                    //fs.Lock(count * (tmp.Length), tmp.Length);
                    //fs.Write(tmp, 0, tmp.Length);
                    //fs.Unlock(count * (tmp.Length), tmp.Length);
                   // baselogin.Add(new string(login).Replace("\0", ""), count);
                }
                
            //}
            }
        }
        public char[] GetPassword(char[] login)
        {
            int id;
            char[] pass=null;
            if (baselogin.TryGetValue(new string(login).Replace("\0", ""),out id ))
            {
                //MySqlDataReader myReader;
                //MySqlCommand myCommand = new MySqlCommand("SELECT Password FROM nowa.masteraccaunts where idMasterAccaunts=@id", connect);
                //MySqlParameter param = new MySqlParameter("id", MySqlDbType.Int32);
                //param.Value = id;

                //myCommand.Parameters.Add(param);
                try
                {
                    // myReader = myCommand.ExecuteReader();
                    var query = from accaunts in DB.masteraccaunts
                                where accaunts.idMasterAccaunts == id
                                select accaunts.Password;


                    //  myReader = myCommand.ExecuteReader();
                    // while (myReader.Read())
                    foreach (var item in query)
                    // while (myReader.Read())
                    {
                        pass = item.ToCharArray();
                    }
                    // myReader.Close();
                }
                catch (Exception)
                {
                }
    
               
                //using (FileStream fs = File.OpenRead(fileName))
                //{
                //    int count = fs.Length / (16 * 2 + 20 * 2);
                //    byte[] bufer = new byte[20 * 2];

                //    fs.Seek((id * (72)) + 32, SeekOrigin.Begin);
                //    fs.Read(bufer, 0, 40);

                //    pass = Encoding.ASCII.GetChars(bufer, 0, 20);


                //}
               
            }
            return pass;
        }

        internal void SetOnline(char[] login)
        {
            int id;
            char[] pass=null;
            if (baselogin.TryGetValue(new string(login).Replace("\0", ""), out id))
            {
                var query = from accaunts in DB.masteraccaunts
                            where accaunts.idMasterAccaunts == id                            
                            select accaunts;
                foreach (var item in query)
               
                {
                    item.Online = true;
                }
                DB.SaveChanges();
                
            }
        }
    }
}
